package com.ciaojian.store.config;


import com.ciaojian.core.factory.ObjectMapperFactory;
import com.ciaojian.core.util.LogUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;


@Component
@Aspect
public class LogConfig {
    @Before(value = "execution(public * com.ciaojian.store.controller.*.*(..))")
    public void businessBefore(JoinPoint joinPoint) throws JsonProcessingException {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        LogUtil.writeBusinessLog("****************************************************************************************************************************************************************************");
        LogUtil.writeBusinessLog("请求接口={}", request.getRequestURL());
        LogUtil.writeBusinessLog("请求方法={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapperInstance();
        if (joinPoint.getArgs().length > 0) {
            for (Object o : joinPoint.getArgs()) {
                if (o instanceof HttpServletRequest || o instanceof HttpServletResponse) {
                }
            }
        }
        String[] headers = {"pragma", "accept", "cache-control", "host", "accept-encoding", "content-type", "connection", "content-length", "user-agent", "postman-token", "upgrade-insecure-requests", "accept-language", "charset", "origin", "referer", "sec-fetch-site", "sec-fetch-mode", "sec-fetch-user", "sec-fetch-dest", "x-forwarded-for"};
        Map<String, Object> headerMap = new LinkedHashMap<>();
        Enumeration<String> headerNames = request.getHeaderNames();
        continueOut:
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            for (String header : headers) {
                if (header.equals(name)) {
                    continue continueOut;
                }
            }
            String value = request.getHeader(name);
            headerMap.put(name, value);
        }
        LogUtil.writeBusinessLog("请求header={}", objectMapper.writeValueAsString(headerMap));

        // 打印参数
        Map<String, Object> map = new LinkedHashMap<>();
        Enumeration pNames = request.getParameterNames();
        while (pNames.hasMoreElements()) {
            String name = (String) pNames.nextElement();
            String value = request.getParameter(name);
            map.put(name, value);
        }
        LogUtil.writeBusinessLog("请求参数={}", "".equals(objectMapper.writeValueAsString(map)) ? "{}" : objectMapper.writeValueAsString(map));
        LogUtil.writeBusinessLog("请求地址={}", request.getRemoteAddr());
        LogUtil.writeBusinessLog("请求方式={}", request.getMethod());
    }

    @AfterReturning(value = "execution(public * com.ciaojian.store.controller.*.*(..))", returning = "value")
    public synchronized void businessAfter(Object value) {
        ObjectMapper om = ObjectMapperFactory.getObjectMapperInstance();
        if (value != null) {
            try {
                String s = om.writeValueAsString(value);
                if (!s.contains("data:image/jpeg;base64") && s.length() < 3000) {
                    LogUtil.writeBusinessLog("响应参数={}", s);
                } else {
                    LogUtil.writeBusinessLog("响应参数={}", "内容过大无法显示");
                }
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }

        }
    }
}
